package 早晨练习0328;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class QMorning {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        File outPath = new File("file/source.txt");
        ensureFileExists(outPath);
        List<Person> list = new ArrayList<>();
        System.out.println("请输入学员信息：");
        Scanner scanner = new Scanner(System.in);
        String input;
        while (true) {
            input = scanner.next();
            if (input.equals(":::")) {
                break;
            }
            list.add(new Person(input));
        }
        writeList2Disk(list, outPath);
        List<Person> listFromDisk = readListFromDisk(outPath);
        System.out.println("请输入要查找的人姓名：");
        String search = scanner.next();
        List<Person> itemList = listFromDisk.stream().filter(item -> item.name.equals(search)).collect(Collectors.toList());
        File parentProperties = new File("file/" + search);
        if (itemList.isEmpty()) {
            System.out.println("未找到相关人员");
            return;
        }
        Person person;
        for (int i = 0; i < itemList.size(); i++) {
            person = itemList.get(i);
            write2Properties(person, new File(parentProperties, search + "_item" + i + ".properties"));
        }
        File[] files = parentProperties.listFiles();
        if (files == null || files.length == 0) {
            return;
        }
        for (File file : files) {
            Properties properties = readFromProperties(file);
            properties.setProperty("age", "40");
            properties.store(new FileWriter(file), "修改");
        }
    }

    public static void ensureFileExists(File file) throws IOException {
        if (!file.exists()) {
            File parentFile = file.getParentFile();
            if (!parentFile.exists()) { parentFile.mkdirs(); }
            file.createNewFile();
        }
    }

    public static void writeList2Disk(List<?> list, File out) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(out));
        oos.writeObject(list);
        oos.close();
    }

    public static <T> List<T> readListFromDisk(File path) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
        Object object = ois.readObject();
        ois.close();
        return (List<T>) object;
    }

    public static void write2Properties(Person person, File outPath) throws IOException {
        ensureFileExists(outPath);
        BufferedWriter bw = new BufferedWriter(new FileWriter(outPath));
        person.tran2Properties().store(bw, "改值");
        bw.close();
    }

    public static Properties readFromProperties(File inPath) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(inPath));
        Properties properties = new Properties();
        properties.load(reader);
        reader.close();
        return properties;
    }

    private static class Person implements Serializable {
        private String name;
        private String age;
        private String sex;

        public Person(String info) {
            Map<String, String> temp = new HashMap<>();
            String[] items = info.split(";");
            for (String item : items) {
                String[] split = item.split(":");
                temp.put(split[0], split[1]);
            }
            this.name = temp.get("name");
            this.age = temp.get("age");
            this.sex = temp.get("sex");
        }

        public Properties tran2Properties() {
            Properties properties = new Properties();
            properties.setProperty("name", name);
            properties.setProperty("age", age);
            properties.setProperty("sex", sex);
            return properties;
        }
    }
}
